package cn.com.wxd.action.website;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.wxd.biz.website.ArticleService;
import cn.com.wxd.entity.website.ArticleInfo;
import cn.com.wxd.service.rss.RomeRSSUtil;
import cn.com.wxd.service.rss.entity.ItemEntity;
import cn.com.wxd.service.rss.entity.RssEntity;
import cn.com.wxd.common.CharUtil;
import cn.com.wxd.common.PageInfo;
import cn.com.wxd.common.data.KVMap;
import cn.com.wxd.util.service.ProjectInfo;
import cn.com.wxd.util.website.WebsiteInfo;

/**
 * Title:RSS源
 * Description:
 *
 * Company: wxd网站系统
 * Copyright: Copyright (c) 2017
 * All right reserved.
 * Created by ComXu on 2017/5/19.
 *
 * @author WangXuDong
 * @version 1.0
 */
@Controller
@RequestMapping("/website")
public class WebsiteRssCtr {
    private static final Logger log = LoggerFactory.getLogger(WebsiteRssCtr.class);
    @Resource
    private ArticleService articleService;

    @RequestMapping(value = "/rss", produces = "text/xml; charset=utf-8")
    @ResponseBody
    public String websiteRss(String caid, String maxCount){


        String rsslevel3page = WebsiteInfo.getRssLevel3Page();
        //从spring中获取service类
        if(maxCount==null||maxCount.trim().equals("")|| CharUtil.stringParsInt(maxCount)==-1){
            maxCount = "20";  //默认显示20条

        }else{
            if(CharUtil.stringParsInt(maxCount)>100){
                maxCount = "100";   //最大显示100条
            }
        }
        if(rsslevel3page==null||rsslevel3page.trim().equals("")){//获取配置文件中的3级页面的地址
            rsslevel3page = "/Level3.do?id=${id}";
        }
        //开始设置网站的RSS信息
        RssEntity re = new RssEntity();
        re.setTitle(WebsiteInfo.getWebsiteName());
        re.setPubDate(new Date());
        re.setLink(ProjectInfo.getDomainName());//设置网站的首页
        re.setLanguage("zh_cn");
        re.setEncoding("utf-8");
        re.setDescription(WebsiteInfo.getDescription());
        re.setCopyright(ProjectInfo.getCopyright());
        //没有栏目时
        if(caid==null||caid.trim().equals("")||CharUtil.stringParsInt(caid)==-1){
            //开始设置网站的RSS文章列表
            KVMap<String, String> order = new KVMap<>();
            order.put("dateTime", "desc");
            //从数据库中获取相应的文章
            List<ArticleInfo> artlist =
                    articleService.getArticleByParam(null,order,null,new PageInfo(1,CharUtil.stringParsInt(maxCount)));
            setRssEntity(rsslevel3page, re, artlist);
        }else{  //有栏目时
            List<ArticleInfo> artlist =
                    articleService.getArticles(caid, new PageInfo(1,CharUtil.stringParsInt(maxCount)));
            setRssEntity(rsslevel3page, re, artlist);
        }
        try {
            String xml = RomeRSSUtil.generateRSSString(re);
            return xml;
        } catch (Exception e) {
            log.error("",e);
        }
        return "";
    }
    /**
     * 设置填充RssEntity
     * @param rsslevel3page
     * @param re
     * @param artlist
     */
    private void setRssEntity(String rsslevel3page, RssEntity re,
                              List<ArticleInfo> artlist) {
        List<ItemEntity> ies = new ArrayList<>();
        for (ArticleInfo anArtlist : artlist) {
            ItemEntity ie = new ItemEntity();
            ie.setTitle(anArtlist.getTitle());
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = df.parse(anArtlist.getDateTime());
                ie.setPubDate(date);
            } catch (Exception e) {
                ie.setPubDate(new Date());
            }
            String levelpath =
                    ProjectInfo.getDomainName() + rsslevel3page.replace("${id}", String.valueOf(anArtlist.getId()));
            ie.setLink(levelpath);
            ie.setDescription(anArtlist.getContent());
            ie.setComments(anArtlist.getRemark());
            ie.setAuthor(anArtlist.getAuthor());
            ies.add(ie);//加入到ies中
        }
        re.setItems(ies);//赋值到re中
    }
}
